{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Homework2\n",
    "###### 这是一个二元分类器\n",
    "- 获取数据:done\n",
    "- 获取样本X,标签y；将X，y的顺序随机打乱：done\n",
    "- 获取训练集60000；测试集10000：done\n",
    "- 获取一个样本数据：39000：2，之后测试用：done\n",
    "- 数据处理：目前MNIST数据集基本都是被处理好的数据集可以直接使用：done;补充：把y_train;y_test转换成int32类型\n",
    "- 引入K-近邻，做fit:K_近邻默认weights：uniform；n_neighbor=5:可以选择：distance和其他的neighbor数组合\n",
    "- GridSearchCV找到最好的参数{'weights':[],'n_neighbors':[]}\n",
    "- precision，recall评估\n",
    "- 要求是精度大于90%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import fetch_mldata\n",
    "# 导入数据\n",
    "mnist=fetch_mldata('mnist-original', data_home='./')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=mnist['data']\n",
    "y=mnist['target']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 拆分数据集\n",
    "X_train,X_test,y_train,y_test=X[:60000,:],X[60000:,:],y[:60000],y[60000:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import sklearn\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 洗牌，重新划分\n",
    "shuffle_index=np.random.permutation(60000)\n",
    "X_train,y_train=X_train[shuffle_index],y[shuffle_index]\n",
    "shuffle_index=np.random.permutation(10000)\n",
    "X_test,y_test=X_test[shuffle_index],y_test[shuffle_index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 随意确定一个值\n",
    "some_digit=X_train[39000]\n",
    "some_digit_img=some_digit.reshape(28,28)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAOEUlEQVR4nO3df6jUdb7H8dfbdJPUUK+n0Dauezeh4ka6DXKhWortiv0As9jYE20mxVlIZSWpW3v/WAkhuVxdQi6We7M126ssmCQkrSaCLcHSFHY8JVutneuePaLHglaDstr3/eN8vZzsfD8zznxnvqPv5wOGmfm+53u+7zPMa74z8/nOfMzdBeD8N6bsBgC0B2EHgiDsQBCEHQiCsANBjG3nxqZNm+YzZ85s5yaBUPr7+3X8+HEbrdZU2M1svqSnJV0g6b/dfXXq9jNnzlS1Wm1mkwASKpVKbq3hl/FmdoGk/5J0q6SrJXWb2dWN/j0ArdXMe/a5kj5090PufkrSVkkLimkLQNGaCftlkv4y4vpAtuwbzKzHzKpmVh0aGmpicwCa0UzYR/sQ4FvH3rr7BnevuHulq6uric0BaEYzYR+QdPmI69+VNNhcOwBapZmwvylplpl9z8y+I+knknYU0xaAojU89ObuX5nZUkm/1/DQ20Z3f7ewzgAUqqlxdnffKWlnQb0AaCEOlwWCIOxAEIQdCIKwA0EQdiAIwg4EQdiBIAg7EARhB4Ig7EAQhB0IgrADQRB2IIi2/pQ0WuO9997LrT311FPJdV988cWmtv3YY48l66tWrcqtjRs3rqlt4+ywZweCIOxAEIQdCIKwA0EQdiAIwg4EQdiBIBhnPwesXp2cHFdr167N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LRNEnH29bA2enU3jq1L4neGlVkb//o7qP+/ltbw/6tjZtV3b1SWgMJndpbp/Yl0Vuj2tUbL+OBIAg7EETZYd9Q8vZTOrW3Tu1LordGtaW3Ut+zA2ifsvfsANqEsANBlBJ2M5tvZn8ysw/N7PEyeshjZv1mdiCbhrpaci8bzeyYmfWNWDbVzHab2QfZ+ahz7JXUW0dM452YZrzU+67s6c/b/p7dzC6Q9L6kf5U0IOlNSd3u/l5bG8lhZv2SKu5e+gEYZvZDSSclveDu/5wt+w9Jn7j76uyJcoq7/1uH9LZS0smyp/HOZiuaPnKacUl3SnpAJd53ib7uURvutzL27HMlfejuh9z9lKStkhaU0EfHc/d9kj45Y/ECSZuyy5s0/GBpu5zeOoK7H3H3t7PLJySdnma81Psu0VdblBH2yyT9ZcT1AXXWfO8uaZeZvWVmPWU3M4pL3f2INPzgkXRJyf2cqeY03u10xjTjHXPfNTL9ebPKCPtoU0l10vjf9e7+A0m3SlqSvVxFfeqaxrtdRplmvCM0Ov15s8oI+4Cky0dc/66kwRL6GJW7D2bnxyRtV+dNRX309Ay62fmxkvv5f500jfdo04yrA+67Mqc/LyPsb0qaZWbfM7PvSPqJpB0l9PEtZjYh++BEZjZB0jx13lTUOyQtyi4vkvRyib18Q6dM4503zbhKvu9Kn/7c3dt+knSbhj+R/7Okfy+jh5y+/knSO9np3bJ7k7RFwy/rvtTwK6IHJf2DpD2SPsjOp3ZQb5slHZDUq+FgTS+ptxs0/NawV9L+7HRb2fddoq+23G8cLgsEwRF0QBCEHQiCsANBEHYgCMIOBEHYgSAIOxDE/wGPizOZqkphXwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "plt.imshow(some_digit_img,cmap=matplotlib.cm.binary)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train=y_train.astype('int32')\n",
    "y_test=y_test.astype('int32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选出为2的\n",
    "y_train_2=(y_train==2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=1, n_neighbors=5, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 距离计算 kn模型\n",
    "kn_clf=KNeighborsClassifier()\n",
    "kn_clf.fit(X_train,y_train_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选定值进行测试\n",
    "some_digit=X_train[39000]\n",
    "some_digit=[some_digit]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试模型\n",
    "kn_clf.predict(some_digit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\program files\\pyvirtual\\homework\\lib\\site-packages\\sklearn\\model_selection\\_split.py:18: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working\n",
      "  from collections import Iterable\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.metrics import precision_score\n",
    "from sklearn.metrics import recall_score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 错误\n",
    "- cross_val_score\n",
    "- cross_val_predict\n",
    "\n",
    "- 当区分错误的时候，容易出现错误：Found input variables with inconsistent numbers of samples: [60000, 3]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 交叉验证\n",
    "y_train_pred=cross_val_score(kn_clf,X_train,y_train_2,cv=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_train_score=y_train_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(60000,)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train_2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9939"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交叉验证 得分\n",
    "y_train_pred[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import cross_val_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_train_pred=cross_val_predict(kn_clf,X_train,y_train_2,cv=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision:98.78%\n",
      "recall:95.22%\n"
     ]
    }
   ],
   "source": [
    "# 精度，召回\n",
    "precision=precision_score(y_train_2,y_train_pred)\n",
    "recall=recall_score(y_train_2,y_train_pred)\n",
    "print('precision:{:.2f}%'.format(precision*100))\n",
    "print('recall:{:.2f}%'.format(recall*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import GridSearchCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=3, error_score=nan,\n",
       "             estimator=KNeighborsClassifier(algorithm='auto', leaf_size=30,\n",
       "                                            metric='minkowski',\n",
       "                                            metric_params=None, n_jobs=None,\n",
       "                                            n_neighbors=5, p=2,\n",
       "                                            weights='uniform'),\n",
       "             iid='deprecated', n_jobs=None,\n",
       "             param_grid=[{'n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n",
       "                          'weights': ['uniform']},\n",
       "                         {'n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n",
       "                          'weights': ['distance']}],\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 网格搜索，调参https://blog.csdn.net/Kyrie_Irving/article/details/90023615\n",
    "param_grid=[\n",
    "    {'weights':['uniform'],'n_neighbors':[i for i in range(1,11)]},\n",
    "    {'weights':['distance'],'n_neighbors':[i for i in range(1,11)]},\n",
    "]\n",
    "final_kn_clf=GridSearchCV(kn_clf,param_grid,cv=3,\n",
    "                         scoring='accuracy')\n",
    "final_kn_clf.fit(X_train,y_train_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 验证\n",
    "final_kn_clf.predict(some_digit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "                     metric_params=None, n_jobs=None, n_neighbors=1, p=2,\n",
       "                     weights='uniform')"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最佳参数\n",
    "final_kn_clf.best_estimator_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 赋值\n",
    "kn_clf_new=KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
    "                     metric_params=None, n_jobs=None, n_neighbors=1, p=2,\n",
    "                     weights='uniform')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 交叉验证\n",
    "y_train_pred=cross_val_predict(kn_clf_new,X_train,y_train_2,cv=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision:98.23%\n",
      "recall:96.04%\n"
     ]
    }
   ],
   "source": [
    "# 精度，召回 \n",
    "precision=precision_score(y_train_2,y_train_pred)\n",
    "recall=recall_score(y_train_2,y_train_pred)\n",
    "print('precision:{:.2f}%'.format(precision*100))\n",
    "print('recall:{:.2f}%'.format(recall*100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_test=y_test.astype('int32')\n",
    "y_test_2=(y_test==2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "precision:97.59%\n",
      "recall:93.99%\n"
     ]
    }
   ],
   "source": [
    "# 测试集  进行验证\n",
    "y_test_pred=cross_val_predict(kn_clf_new,X_test,y_test_2,cv=3)\n",
    "precision=precision_score(y_test_2,y_test_pred)\n",
    "recall=recall_score(y_test_2,y_test_pred)\n",
    "print('precision:{:.2f}%'.format(precision*100))\n",
    "print('recall:{:.2f}%'.format(recall*100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Conclusion\n",
    "- KNeighborsClassfier\n",
    "    - precision：0.9878112484764061\n",
    "    - recall：0.9521651560926485\n",
    "    \n",
    "- GridSearchCV:best_estimator:\n",
    "    - precision:98.23%\n",
    "    - recall:96.04%\n",
    "- test_score\n",
    "    - precision:97.59%\n",
    "    - recall:93.99%"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
